using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace SpaceInvaders
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Player : Sprite
    {
        public bool canShoot = true;
        public bool alive = true;
        public Projectile projectile;

        public void Fire()
        {
            projectile = new Projectile(Game, 5, 10, "bullet")
                             {position = new Vector2(position.X + width/2-2, position.Y - height)};
            Game.Components.Add(projectile);
            
            canShoot = false;
        }

        public Player(Game game, int width, int height, String location)
            : base(game, width, height, location)
        {
            position = new Vector2(Game1.screenWidth/2 - width/2, Game1.screenHeight-height);
            velocity = new Vector2(6,0);
            
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }


        protected override void LoadContent()
        {
            
            base.LoadContent();
        }

        public bool WithinBounds(int direction)
        {
            switch(direction)
            {
                    //going left
                case -1:
                    {
                        return position.X - velocity.X >= 0;
                    }
                    //going right
                case 1:
                    {
                        return position.X + width + velocity.X < Game1.screenWidth;
                    }
                default:
                    return false;
            }
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            KeyboardState keyState = Keyboard.GetState();
            if (projectile != null && projectile.OutOfBounds())
            { 
                projectile = null;
                canShoot = true;
            }

            if(keyState.IsKeyDown(Keys.Right))
            {
                if(WithinBounds(1))
                {
                    position += velocity;
                }
            }

            if (keyState.IsKeyDown(Keys.Left))
            {
                if (WithinBounds(-1))
                {
                    position -= velocity;
                }
            }

            if (keyState.IsKeyDown(Keys.Space) && canShoot)
            {
                Fire();
            }

            base.Update(gameTime);
        }
    }
}
